package com.example.leetcode;

import java.util.*;
public class HW1301 {
    public static void main(String[] args)
    {


               ListNode head = new ListNode(0);
               ListNode tmp = head;
               for (int i=0; i<8; i++)
               {
                   tmp.next = new ListNode(i);
                   tmp = tmp.next;
               }


              ListNode res = findLastKNode(head,3);
             System.out.println(res.val);



        }

    //查找链表倒数第k个点
    public static ListNode findLastKNode(ListNode head, int k)


    {
       /* ListNode slow = head;
        for (int i=0; i<8-k+1; i++)
        {
            slow = slow.next;
        }
*/
        //获取链表正数第几个
        ListNode fast = head;
        //通过正数，获取倒数
        ListNode slow = head;
        for (int i=0; i<k; i++)
        {
            fast = fast.next;
        }
        while (fast != null)
        {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //链表节点定义
    static  class ListNode
    {
        int val;
        ListNode next = null;
        ListNode(int x)
        {
            this.val = x;
        }
    }


}
